<template>
  <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleFormRef"
      label-width="100px"
    >
      <el-form-item label="广告图片:" prop="imageUrl">
        <div v-if="!ruleForm.imageUrl">
          <el-upload
            class="avatar-uploader"
            action="http://blog.9yuecloud.com/pro-api/article/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <img :src="ruleForm.imageUrl" alt="" v-else />
      </el-form-item>
      <el-form-item label="广告标题:" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="广告链接:" prop="advertUrl">
        <el-input v-model="ruleForm.advertUrl"></el-input>
      </el-form-item>
      <el-form-item label="跳转方式:" prop="advertTarget">
        <el-select
          v-model="ruleForm.advertTarget"
          placeholder="请选择"
          style="width:90px"
        >
          <el-option
            :label="item.title"
            :value="item.val"
            v-for="item in advertTargets"
            :key="item.val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="ruleForm.status" size="medium">
          <el-radio :label="item.val" v-for="item in options" :key="item.val">{{
            item.title
          }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="广告位置:" prop="position">
        <el-input-number v-model="ruleForm.position"></el-input-number>
      </el-form-item>
      <el-form-item label="排序:" prop="sort">
        <el-input-number v-model="ruleForm.sort"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // RuleForm: Object,
    dialogVisible: {
      type: Boolean,
      default: () => {
        return false;
      }
    },
    dialogTitle: {
      type: String,
      default: () => {
        return "新增";
      }
    }
  },
  data() {
    return {
      options: [
        { val: 0, title: "禁用" },
        { val: 1, title: "正常" }
      ],
      advertTargets: [
        { val: "_self", title: "当前窗口" },
        { val: "_blank", title: "新窗口" }
      ],
      ruleForm: {
        advertTarget: "",
        advertUrl: "",
        imageUrl: "",
        position: "",
        sort: "",
        status: "",
        title: ""
      },
      rules: {
        title: [{ required: true, message: "请输入广告标题", trigger: "blur" }],
        imageUrl: [
          { required: true, message: "请上传广告图片", trigger: "blur" }
        ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        advertUrl: [
          { required: true, message: "请输入广告链接", trigger: "blur" }
        ],
        position: [
          { required: true, message: "请输入广告位置", trigger: "blur" }
        ],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }]
      },
      // options: Array
      imageUrl: ""
    };
  },
  methods: {
    // 图片上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    // 新增 编辑 提交
    submit() {
      this.$refs.ruleFormRef.validate(valid => {
        if (valid) {
          if (this.dialogTitle == "新增") {
            this.$emit("addItem", this.ruleForm);
          } else {
            this.$emit("editItem", this.ruleForm);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 打开dialog时清空表单信息
    beforeResetForm() {
      this.$nextTick(() => {
        this.$refs.ruleFormRef.resetFields();
      });
    },
    // 编辑后表单信息
    changeRuleForm(e) {
      console.log(e);
      this.ruleForm = e;
    },
    handleAvatarSuccess() {}
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
img {
  width: 178px;
  height: 178px;
}
</style>
